

%
%% Poaching index 
[~,poachpos] = sort(AvgEEShare);	% "poachpos" ranks firm IDs from lowest to highest AvgEEShare.
 % eg, if 15th firm has highest AvgEEShare, then *final*
 % element of poachpos is 15. Thus, AvgEEshare(poachpos)
 % is the ranked vector.
 %
 %%%%  For making rankings:
bins = 10;
poachbin = (.1 :.1: 1)';
 %
 %%%%  Employment distribution over poaching rank
EmpLevel   = sum(EmpEndofmo, 2);
EmpDistrib = cumsum(EmpLevel(poachpos)) / sum(EmpLevel); 
 


%
%% Flows and wages by poaching rank.
AvgLayoffRate = mean(LayoffRate3mo, 2);
AvgQuitRate   = mean(QuitRate3mo, 2); 
 %
 % Ranking of wages:
endpos    = (12 :12: months);
AvgWage   = mean(WageEndofmo(:, endpos), 2);
[~,wagepos] = sort(AvgWage);
WageRank    =  zeros(NumFirms,1);
WageRank(wagepos) = (1:NumFirms)' /NumFirms;
 %
EUPoach  = zeros(bins,1);
EEPoach  = zeros(bins,1);
WagePoach  = zeros(bins,1);
 %
wpos1 = 1;
for i = 1:bins
     % Identify firms employing workers in the ith decile  
     % of the distribution of workers over poaching rank.
    [~, wpos2]  = min(abs(EmpDistrib - poachbin(i)));
    poachros    = poachpos(wpos1:wpos2);
    eweight     = EmpLevel(poachros) / sum(EmpLevel(poachros));
    %    
    % a) Use avg. size for weighting w/in bin
    EUPoach(i)  = eweight'*AvgLayoffRate(poachros);
    EEPoach(i)  = eweight'*AvgQuitRate(poachros);
    WagePoach(i)= eweight'*WageRank(poachros);
    wpos1 = wpos2 +1;
end
PoachBinMAT   = [poachbin,  WagePoach, EUPoach, EEPoach];
